<template>
  <div>
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getSelcetList()"
    >
      <el-form-item prop="name">
        <el-input
          v-model="dataForm.name"
          placeholder="姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
          v-model="dataForm.username"
          placeholder="账号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="departmentId">
        <!-- 部门级联组件 -->
        <department-select-tree
          @input="getID"
          style="width: 250px"
          v-model="dataForm.departmentId"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="getSelcetList()">查询</el-button>
        <el-button
          v-if="isAuth('feature:repair:add')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('feature:repair:delete')"
          type="danger"
          :disabled="dataListSelections.length <= 0"
          @click="deleteHandle()"
          >批量删除</el-button
        >
        <el-button type="info" @click="dataForm = {}">重置</el-button>
        <el-button type="info" @click="refresh">刷新</el-button>
      </el-form-item>
      <el-form-item>
        <!-- 表格的导入导出 -->
        <el-button @click="showExcelDialog = true">excel导入</el-button>
        <el-button @click="exportEmployee">excel导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column prop="personalNumber" width="150" label="学号/工号">
      </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="name" label="真实姓名"> </el-table-column>
      <el-table-column prop="mobile" label="电话号码"> </el-table-column>
      <el-table-column prop="departmentName" label="部门"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status === 0"
            >封禁</el-tag
          >
          <el-tag type="info" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('feature:repair:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.userId)"
            >修改</el-button
          >
          <el-button
            v-if="isAuth('feature:repair:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.userId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- Excel弹层 -->
    <repair-user-excel
      :uploadSuccess="uploadFinish"
      :show-excel-dialog.sync="showExcelDialog"
    />
    <!-- 新增 / 修改弹层 -->
    <repair-add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import RepairUserExcel from './repair-user-excel.vue'
import RepairAddOrUpdate from './repair-add-or-update.vue'
import DepartmentSelectTree from './department-select-tree.vue'
export default {
  components: {
    RepairUserExcel,
    RepairAddOrUpdate,
    DepartmentSelectTree
  },
  data() {
    return {
      dataListSelections: [],
      dataList: [],
      dataForm: {
        name: '',
        username: '',
        departmentId: null
      },
      dataListLoading: false,
      showExcelDialog: false, // 打开Excel对话框
      addOrUpdateVisible: false,
      // 分页
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      // 部门信息
      options: []
    }
  },
  methods: {
    uploadFinish() {
      this.getDataList()
    },
    getDataList() {
      // 获取数据
      this.$http({
        url: this.$http.adornUrl('sys/visitor/getVisitorList'),
        method: 'post',
        data: this.$http.adornData({
          ...this.dataForm,
          type: 5,
          page: this.pageIndex,
          size: this.pageSize
        })
      }).then(({ data }) => {
        this.dataList = data.data
        this.totalPage = data.total
      })
    },
    exportEmployee() {
      console.log('下载Excel')
      this.$http({
        url: this.$http.adornUrl('sys/repairman/exportRepairman'),
        method: 'post',
        data: this.dataForm,
        responseType: 'blob'
      }).then(({ data }) => {
        const link = document.createElement('a')
        let blob = new Blob([data], { type: 'application/vnd.ms-excel' })
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        link.setAttribute('download', '导出维修人员信息表')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 条件查询
    getSelcetList() {
      this.getDataList()
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId
          })
      this.$confirm(
        `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          console.log(ids)
          this.$http({
            url: this.$http.adornUrl('sys/visitor/deleteVisitorByIds'),
            method: 'put',
            params: { ids },
            // 主要是以下四行代码 , 传数组参数
            paramsSerializer: function (params) {
              const ids = params.ids.map((_) => `ids=${_}`).join('&')
              return `${ids}`
            }
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    },
    getID(id) {
      this.dataForm.departmentId = id
    },
    // 刷新
    refresh() {
      //this.dataForm = {}
      this.getDataList()
    }
  },
  created() {
    this.getDataList()
  }
}
</script>

<style lang="scss" scoped>
.opeate-tools {
  margin: 10px;
}
.cell-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
